package com.twetec.cms.service.website.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.dao.mapper.website.ArticleExtendMapper;
import com.twetec.cms.model.po.website.ArticleExtendPO;
import com.twetec.cms.model.po.website.ArticlePO;
import com.twetec.cms.service.website.ArticleExtendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 * 文章扩展表 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-10-08
 */
@Service
public class ArticleExtendServiceImpl extends ServiceImpl<ArticleExtendMapper, ArticleExtendPO> implements ArticleExtendService {

    @Autowired
    private ArticleExtendMapper articleExtendMapper;

    @Override
    public boolean saveOrUpdateArticleExtend(Long articleId, String content, String seoTitle, String seoKeywords, String seoDesc, String extendFieldListJason, Long userId) {
        ArticleExtendPO articleExtendPO = new ArticleExtendPO();

        articleExtendPO.setModifierId(userId);
        // 内容
        articleExtendPO.setContent(content);
        // seoTitle
        articleExtendPO.setSeoTitle(seoTitle);
        // seoKeywords
        articleExtendPO.setSeoKeywords(seoKeywords);
        // seoDesc
        articleExtendPO.setSeoDesc(seoDesc);
        // extendFieldList扩展字段
        articleExtendPO.setExtendFieldList(extendFieldListJason);
        // 文章id
        articleExtendPO.setArticleId(articleId);

        // 根据id查询是否有记录
        QueryWrapper<ArticleExtendPO> articleExtendPOQueryWrapper = new QueryWrapper<>();
        articleExtendPOQueryWrapper.eq("ARTICLE_ID", articleId);
        articleExtendPOQueryWrapper.last(" AND ROWNUM = 1");
        ArticleExtendPO articleExtendPOById = articleExtendMapper.selectOne(articleExtendPOQueryWrapper);
        int update = 0;
        int insert = 0;
        if (!ValidateUtil.isNullOrEmpty(articleExtendPOById)) {
            // 查询到记录的时候,设置id,并进行更新
            articleExtendPO.setId(articleExtendPOById.getId());
             update= articleExtendMapper.updateById(articleExtendPO);
        } else {
            // 没查询到记录，设置创建人id，并进行插入
            articleExtendPO.setCreatorId(userId);
            insert = articleExtendMapper.insert(articleExtendPO);
        }
        if (update > 0 || insert > 0) return true;
        return false;
    }

    @Override
    public boolean replace(ArrayList<String> articleIdList, String originContent, String replaceContent) {
        // 替换标题
        UpdateWrapper<ArticleExtendPO> articleExtendPOUpdateWrapper = new UpdateWrapper<>();
        articleExtendPOUpdateWrapper.in("ARTICLE_ID", articleIdList);
        articleExtendPOUpdateWrapper.setSql("CONTENT = REPLACE(CONTENT, '" + originContent + "','" + replaceContent + "')");

        return this.update(articleExtendPOUpdateWrapper);
    }
}
